package adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.menhoo.toollibrary.common.helper.MessageUtil;
import com.example.menhoo.toollibrary.common.utils.StringUtil;
import com.lidroid.xutils.BitmapUtils;
import com.zejian.emotionkeyboard.utils.EmotionUtils;
import com.zejian.emotionkeyboard.utils.SpanStringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import application.HttpUrlConstant;
import application.UIApplication;
import menhoo.com.i8officelibrary.R;
import model.IMCvnDBModel;

/**
 * Created by yanglei on 2017/3/23.
 */

public class ImAdapter extends RecyclerView.Adapter<ImAdapter.ImViewHolder>{

    private LayoutInflater inflater;
    private Context context;
    private List<IMCvnDBModel> list=new ArrayList<>();
    private OnItemClickLitener mOnItemClickLitener;
    BitmapUtils bitmapUtils = null;

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    public ImAdapter(Context context){
        this.context = context;
        this.inflater = LayoutInflater.from(this.context);
        this.list = new ArrayList<>();
        this.bitmapUtils =new BitmapUtils(context, UIApplication.getAppCachePath(context));
    }

    public IMCvnDBModel getItem(int position) {
        if (this.list != null) {
            return this.list.get(position);
        } else {
            return null;
        }

    }
    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public List<IMCvnDBModel> getData(){
        return this.list;
    }
    public void remove(IMCvnDBModel item){
        this.getData().remove(item);
    }
    public  IMCvnDBModel getItemByGroupID(String groupID){
        if (this.list != null) {
            for(IMCvnDBModel item : this.list){
                if(item.groupID.equalsIgnoreCase(groupID)){
                    return item;
                }
            }
            return null;
        } else {
            return null;
        }
    }
    public void add(List<IMCvnDBModel> data) {
        if (this.list != null) {
            this.list.addAll(data);
            notifyDataSetChanged();
        }
    }
    public void add(int p,IMCvnDBModel item) {
        if (this.list != null) {
            this.list.add(p,item);
            notifyDataSetChanged();
        }
    }
    public void clear(){
        if (this.list != null) {
            this.list.clear();
        }
        notifyDataSetChanged();
    }
    public void refresh(List<IMCvnDBModel> data){
        if (this.list != null) {
        this.list=data;
        }
        notifyDataSetChanged();
    }
    public void sort(){
        Collections.sort(this.list, new Comparator<IMCvnDBModel>() {
            @Override
            public int compare(IMCvnDBModel lhs, IMCvnDBModel rhs) {

                return rhs.lastTime.compareTo(lhs.lastTime);
            }
        });
    }
    public void update(List<IMCvnDBModel> data){
        for(IMCvnDBModel item : data){
            boolean F = false;
            for(IMCvnDBModel cvn : this.list){
                if(cvn.id.equalsIgnoreCase(item.id)){
                    cvn.name = item.name;
                    cvn.lastMessage = item.lastMessage;
                    cvn.unreadCount = item.unreadCount;
                    cvn.userCount = item.userCount;
                    cvn.lastTime = item.lastTime;
                    F=true;
                    break;
                }
            }
            if(!F){
                this.list.add(0,item);
            }
        }
        sort();
        notifyDataSetChanged();
    }
    public void update(IMCvnDBModel item){

        for(IMCvnDBModel cvn : this.list){
            if(cvn.id.equalsIgnoreCase(item.id)){

                cvn.name = item.name;
                cvn.lastMessage = item.lastMessage;
                cvn.unreadCount = item.unreadCount;
                cvn.userCount = item.userCount;
                cvn.lastTime = item.lastTime;

                sort();
                notifyDataSetChanged();

                return;
            }
        }


    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ImViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_item_im_cvn,parent,false);
        ImViewHolder imViewHolder = new ImViewHolder(view,viewType);
        return imViewHolder;
    }

    @Override
    public void onBindViewHolder(final ImViewHolder holder, int position) {
        final IMCvnDBModel model = getItem(position);
        if(model==null)
            return;
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

        try{

            if(!StringUtil.isEmpty(model.imgUrl)){
                bitmapUtils.display(holder.user_img, HttpUrlConstant.getWebSiteFullUrl(model.imgUrl));
            }

            if(model.type.toLowerCase().equalsIgnoreCase("group")){
                holder.textViewName.setText(model.name+" ("+model.userCount+")");
            }
            else{
                holder.textViewName.setText(model.name);
            }

            if(model.unreadCount>0){
                if(model.unreadCount>99){
                    holder.textViewNoReadCount.setText("99+");
                }
                else {
                    holder.textViewNoReadCount.setText(model.unreadCount+"");
                }

                holder.linearLayoutNoreadCount.setVisibility(View.VISIBLE);
            }
            else{
                holder.linearLayoutNoreadCount.setVisibility(View.GONE);
            }
            holder.textViewTime.setText(model.shortTime);
            if(StringUtil.isEmpty(model.lastMessage)){
                holder.textViewMessage.setText("还没有说过话哦");
            }
            else{
                //holder.textViewMessage.setText(model.lastMessage);
                holder.textViewMessage.setText(SpanStringUtils.getEmotionContent(EmotionUtils.EMOTION_CLASSIC_TYPE,context,holder.textViewMessage,model.lastMessage));
            }
        }
        catch (Exception ex){
            MessageUtil.showShortToast(context,""+ex.getMessage());
        }

    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }

    class ImViewHolder extends RecyclerView.ViewHolder{
        //Text
        public TextView textViewNoReadCount,textViewName,textViewTime,textViewMessage;
        public RelativeLayout relativeLayoutImage;
        public ImageView user_img;
        public LinearLayout linearLayoutNoreadCount;

        public ImViewHolder(View itemView) {
            super(itemView);
        }

        public ImViewHolder(View itemView,int viewType) {
            super(itemView);
            user_img = (ImageView) itemView.findViewById(R.id.user_img);
            textViewNoReadCount = (TextView) itemView.findViewById(R.id.textViewNoReadCount);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewTime = (TextView) itemView.findViewById(R.id.textViewTime);
            textViewMessage = (TextView) itemView.findViewById(R.id.textViewMessage);
            linearLayoutNoreadCount = (LinearLayout) itemView.findViewById(R.id.linearLayoutNoreadCount);
        }
    }
}
